using Godot;
using System;

public class WelcomeView : Node
{
	public enum Mode
	{
		New,
		Open,
	}
	const string ProjectRoot = "C:/Users/wuchunlin/Documents/projects/";
	// Declare member variables here. Examples:
	private Mode _mode;
	public FileDialog FileDialog { get; set; }
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		if (!System.IO.Directory.Exists(ProjectRoot))
			System.IO.Directory.CreateDirectory(ProjectRoot);
		FileDialog = this.GetNode<FileDialog>("FileDialog");
		FileDialog.CurrentDir = ProjectRoot;
		FileDialog.CurrentPath = ProjectRoot;
		FileDialog.Connect("file_selected", this, nameof(OnFileSelected));
		GD.Print("FileDialog", FileDialog);
	}

	private void OnFileSelected(string path)
	{
		switch (_mode)
		{
			case Mode.New:
				FileSystem.New(path);
				break;
			case Mode.Open:
				FileSystem.Open(path);
				break;
			default:
				GD.PrintErr("未知模式！");
				break;
		}
	}

	private void _on_NewProject_pressed()
	{
		_mode = Mode.New;
		FileDialog.WindowTitle = "新建项目";
		FileDialog.Mode = FileDialog.ModeEnum.SaveFile;
		FileDialog.PopupCentered();

	}
	private void _on_OpenProject_pressed()
	{
		_mode = Mode.Open;
		FileDialog.WindowTitle = "打开项目";
		FileDialog.Mode = FileDialog.ModeEnum.OpenFile;
		FileDialog.PopupCentered();
	}
}
